﻿using System;
using ZFCNew.ZfcServiceReference;
using System.Collections.ObjectModel;
using Facebook.Schema;

namespace ZFCNew.Code
{
    public class ServiceHelper
    {
        #region Images
        public event EventHandler<DataEventArgs<ObservableCollection<ImageInfo>>> GetImageCompleted;

        public void GetImage()
        {
            var proxy = new ZfcServiceClient();
            proxy.GetImagesCompleted += new EventHandler<GetImagesCompletedEventArgs>(ProxyGetImagesCompleted);
            proxy.GetImagesAsync();
        }

        private void ProxyGetImagesCompleted(object sender, GetImagesCompletedEventArgs e)
        {
            ObservableCollection<ImageInfo> res = null;
            if (e.Error == null && !e.Cancelled)
            {
                res = e.Result;
            }
            if (GetImageCompleted != null)
            {
                GetImageCompleted(this, new DataEventArgs<ObservableCollection<ImageInfo>> { Data = res, IsSuccess = res != null });
            }
        }
        #endregion

        #region User
        public event EventHandler<DataEventArgs<int>> UpdateUserCompleted;
        public void UpdateUser(user user)
        {
            UserInfo dbUser = Utility.ConvertToDbUser(user);
            var proxy = new ZfcServiceClient();
            proxy.SaveUserInfoCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ProxySaveUserInfoCompleted);
            proxy.SaveUserInfoAsync(dbUser);
        }
        private void ProxySaveUserInfoCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {

            if (e.Error == null)
            {
                if (UpdateUserCompleted != null)
                {
                    UpdateUserCompleted(this, new DataEventArgs<int> { Data = 0, IsSuccess = true });
                }
            }
        }
        #endregion

        #region Feedback
        public event EventHandler<DataEventArgs<int>> FeedbackSent;
        public void SendFeedback(Feedback feedback)
        {
            var proxy = new ZfcServiceClient();
            proxy.AddFeedbackCompleted += ProxyAddFeedbackCompleted;
            proxy.AddFeedbackAsync(feedback);
        }

        void ProxyAddFeedbackCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (FeedbackSent != null)
            {
                FeedbackSent(this, new DataEventArgs<int> { Data = 0, IsSuccess = e.Error == null });
            }

        }

        #endregion

        #region Team
        public event EventHandler<DataEventArgs<ObservableCollection<Team>>> GetTeamsCompleted;
        public void GetTeams()
        {
            var proxy = new ZfcServiceClient();
            proxy.GetTeamsCompleted += proxy_GetTeamsCompleted;
            proxy.GetTeamsAsync();
        }

        void proxy_GetTeamsCompleted(object sender, GetTeamsCompletedEventArgs e)
        {
            if(e.Error==null && !e.Cancelled)
            {
                if(GetTeamsCompleted!=null)
                {
                    GetTeamsCompleted(this, new DataEventArgs<ObservableCollection<Team>> {Data = e.Result});
                }
            }
        }

       

        #endregion

        #region Matches
        
        public event EventHandler<DataEventArgs<ObservableCollection<Schedule>>> GetAllMatchesCompleted;
        public void GetAllMatches()
        {
            var proxy = new ZfcServiceClient();
            proxy.GetAllMatchesCompleted += new EventHandler<GetAllMatchesCompletedEventArgs>(proxy_GetAllMatchesCompleted);
            proxy.GetAllMatchesAsync();
        }

        void proxy_GetAllMatchesCompleted(object sender, GetAllMatchesCompletedEventArgs e)
        {
            if(e.Error == null && !e.Cancelled)
            {
                if(GetAllMatchesCompleted!=null)
                {
                    GetAllMatchesCompleted(this,new DataEventArgs<ObservableCollection<Schedule>>{Data = e.Result});
                }
            }
        }

        #endregion

        #region NextMatch
        
        public event EventHandler<DataEventArgs<Schedule>> GetNextMatchCompleted;
        public void GetNextMatch()
        {
            var proxy = new ZfcServiceClient();
            proxy.GetNextMatchCompleted+=new EventHandler<GetNextMatchCompletedEventArgs>(proxy_GetNextMatchCompleted);
            proxy.GetNextMatchAsync();
        }

        void proxy_GetNextMatchCompleted(object sender, GetNextMatchCompletedEventArgs e)
        {
            if(e.Error == null && !e.Cancelled)
            {
                if(GetNextMatchCompleted!=null)
                {
                    GetNextMatchCompleted(this, new DataEventArgs<Schedule>{Data = e.Result});
                }
            }
        }

        #endregion

    }

    public class DataEventArgs<T> : EventArgs
    {
        public T Data { get; set; }
        public bool IsSuccess { get; set; }
    }
}
